package com.shenyong.ladmin.controller;

import com.shenyong.controller.BaseController;
import com.shenyong.entity.LoginUsers;
import com.shenyong.entity.Notice;
import com.shenyong.ladmin.logConfig.Log;
import com.shenyong.ladmin.logConfig.OperationType;
import com.shenyong.ladmin.service.NoticeService;
import com.shenyong.service.UserService;
import com.shenyong.util.JwtUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.Map;

/**
 * 后台公告管理
 * noticeAll
 * noticeSave
 * noticeDelete
 */
@RestController
public class NoticeController {
  @Autowired
  private UserService userService;
  @Autowired
  private BaseController baseController;
  @Resource
  private NoticeService noticeService;
  @RequiresAuthentication
  @ApiOperation(value = "查询公告", notes = "分页查询公告", produces = "application/json")
  @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "起始行", required = false, dataType = "int")
          , @ApiImplicitParam(name = "limit", value = "显示多少条数据", required = false, dataType = "int")})
  @RequestMapping("noticeAll")
  public Map<String, Object> getAll(int page, int limit) {
    Map<String, Object> mapn = new HashMap<String, Object>();
    mapn.put("page", (page - 1) * limit);
    mapn.put("limit", limit);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("msg", 0);
    map.put("code", "");
    map.put("count", noticeService.noticeCount());
    map.put("data", noticeService.findAll(mapn));
    System.out.println(map+"map");
    return map;
  }
  @RequiresAuthentication
  @Log(describe = "添加一个公告",operationType= OperationType.INSERT)
  @RequestMapping("noticeSave")
  public Object getSave(Notice notice) {
    if (notice.getNotice_id() == null || notice.getNotice_id().equals(0)){
      String nickName1= baseController.getToken();
      String nickName= JwtUtil.getUsername(nickName1);
      LoginUsers user= userService.getName(nickName);
      notice.setNotice_administratorId(user.getUser().getUser_id());
      return noticeService.save(notice);

    }else{
      return noticeService.noticeUpdate(notice);
    }
  }
  @RequiresAuthentication
  @Log(describe = "删除一个公告",operationType= OperationType.DELETE)
  @RequestMapping("noticeDelete")
  public Object noticeDelete(int notice_id) {
    return noticeService.noticeDelete(notice_id);
  }

}
